<template>
  <div class="promotions-container">
    <h2>促销活动</h2>

    <!-- 活动分类标签 -->
    <div class="promotion-tabs">
      <button 
        v-for="tab in tabs" 
        :key="tab.value"
        @click="currentTab = tab.value"
        :class="{ active: currentTab === tab.value }"
        class="tab-btn"
      >
        {{ tab.label }}
      </button>
    </div>

    <!-- 促销活动列表 -->
    <div class="promotions-grid">
      <div v-for="promotion in filteredPromotions" :key="promotion._id" class="promotion-card">
        <div class="promotion-image">
          <img :src="promotion.image" :alt="promotion.name">
          <div class="promotion-tag" :class="promotion.type">
            {{ getPromotionTag(promotion.type) }}
          </div>
        </div>
        <div class="promotion-content">
          <h3>{{ promotion.name }}</h3>
          <p class="promotion-description">{{ promotion.description }}</p>
          <div class="promotion-period">
            {{ formatDate(promotion.startDate) }} - {{ formatDate(promotion.endDate) }}
          </div>
          <div class="promotion-details">
            <div class="detail-item" v-if="promotion.type === 'discount'">
              <span class="label">折扣力度:</span>
              <span class="value">{{ promotion.discountRate }}折</span>
            </div>
            <div class="detail-item" v-if="promotion.type === 'special_price'">
              <span class="label">特价:</span>
              <span class="value">¥{{ promotion.specialPrice }}</span>
            </div>
            <div class="detail-item" v-if="promotion.minPurchase">
              <span class="label">最低消费:</span>
              <span class="value">¥{{ promotion.minPurchase }}</span>
            </div>
          </div>
          <router-link 
            :to="{ name: 'Products', query: { promotionId: promotion._id }}" 
            class="view-products-btn"
          >
            查看活动商品
          </router-link>
        </div>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading">
      加载中...
    </div>

    <!-- 错误提示 -->
    <div v-if="error" class="error-message">
      {{ error }}
    </div>

    <!-- 无数据提示 -->
    <div v-if="!loading && filteredPromotions.length === 0" class="empty-state">
      当前没有{{ getTabLabel(currentTab) }}的促销活动
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import api from '@/api'

interface Promotion {
  _id: string
  name: string
  description: string
  type: 'discount' | 'special_price' | 'bundle'
  image: string
  startDate: string
  endDate: string
  discountRate?: number
  specialPrice?: number
  minPurchase?: number
  status: 'upcoming' | 'active' | 'ended'
}

const tabs = [
  { label: '全部活动', value: 'all' },
  { label: '正在进行', value: 'active' },
  { label: '即将开始', value: 'upcoming' },
  { label: '已结束', value: 'ended' }
]

const promotions = ref<Promotion[]>([])
const currentTab = ref('active')
const loading = ref(false)
const error = ref('')

// 根据活动类型获取标签文本
const getPromotionTag = (type: string) => {
  const tags = {
    discount: '折扣',
    special_price: '特价',
    bundle: '套装'
  }
  return tags[type as keyof typeof tags]
}

// 获取标签文本
const getTabLabel = (value: string) => {
  return tabs.find(tab => tab.value === value)?.label || ''
}

// 格式化日期
const formatDate = (date: string) => {
  return new Date(date).toLocaleDateString('zh-CN')
}

// 根据当前标签筛选促销活动
const filteredPromotions = computed(() => {
  if (currentTab.value === 'all') {
    return promotions.value
  }
  return promotions.value.filter(p => p.status === currentTab.value)
})

// 获取促销活动列表
const fetchPromotions = async () => {
  loading.value = true
  error.value = ''
  
  try {
    const response = await api.get('/promotions')
    promotions.value = response.data
  } catch (err: any) {
    error.value = '获取促销活动列表失败'
  } finally {
    loading.value = false
  }
}

onMounted(fetchPromotions)
</script>

<style scoped>
.promotions-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.promotion-tabs {
  margin: 20px 0;
  display: flex;
  gap: 10px;
}

.tab-btn {
  padding: 8px 16px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  background: white;
  cursor: pointer;
  transition: all 0.3s;
}

.tab-btn.active {
  background: #1890ff;
  color: white;
  border-color: #1890ff;
}

.promotions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 20px;
}

.promotion-card {
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  overflow: hidden;
  background: white;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.promotion-image {
  position: relative;
  height: 200px;
}

.promotion-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.promotion-tag {
  position: absolute;
  top: 10px;
  right: 10px;
  padding: 4px 8px;
  border-radius: 4px;
  color: white;
  font-size: 12px;
}

.promotion-tag.discount {
  background-color: #ff4d4f;
}

.promotion-tag.special_price {
  background-color: #722ed1;
}

.promotion-tag.bundle {
  background-color: #52c41a;
}

.promotion-content {
  padding: 20px;
}

.promotion-content h3 {
  margin: 0 0 10px 0;
  color: #333;
}

.promotion-description {
  color: #666;
  font-size: 14px;
  margin-bottom: 10px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.promotion-period {
  color: #999;
  font-size: 12px;
  margin-bottom: 15px;
}

.promotion-details {
  margin-bottom: 15px;
}

.detail-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 5px;
  font-size: 14px;
}

.detail-item .label {
  color: #666;
}

.detail-item .value {
  color: #ff4d4f;
  font-weight: bold;
}

.view-products-btn {
  display: block;
  width: 100%;
  padding: 10px;
  text-align: center;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  text-decoration: none;
  transition: all 0.3s;
}

.view-products-btn:hover {
  background-color: #40a9ff;
}

.loading {
  text-align: center;
  padding: 20px;
  color: #666;
}

.error-message {
  color: #ff4d4f;
  text-align: center;
  padding: 10px;
  margin-top: 10px;
}

.empty-state {
  text-align: center;
  padding: 40px;
  color: #999;
  font-size: 14px;
}
</style> 